﻿Imports System.Data.OleDb

Public Class BeachesList
    Inherits System.Web.UI.Page


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'Assign text to the heading
        myHeading.InnerHtml = "List of Beaches"

        If IsPostBack = False Then
            'Populate the tag cloud
            TagCloud.getTagCloud(SqlDataSource3, CloudTags, Page)

            'Count Activity for page
            Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("BeachConnectionString").ConnectionString)
            Dim SqlStringActivity As String = "INSERT INTO SiteActivity(Activity) VALUES (@f1)"
            Dim cmdActivity As OleDbCommand = New OleDbCommand(SqlStringActivity, oleDbConn)
            cmdActivity.CommandType = CommandType.Text
            cmdActivity.Parameters.AddWithValue("@f1", "Beaches")
            oleDbConn.Open()
            cmdActivity.ExecuteNonQuery()
            oleDbConn.Close()
        End If
    End Sub

    Protected Sub btn_findByName_Click(sender As Object, e As EventArgs) Handles btn_findByName.Click
        searchDatabase("BeachName", tb_search.Text)
    End Sub

    Protected Sub btn_findByRegion_Click(sender As Object, e As EventArgs) Handles btn_findByRegion.Click
        searchDatabase("Region", tb_search.Text)
    End Sub

    Private Function searchDatabase(ByVal buttonStr As String, ByVal searchStr As String)
        SqlDataSource2.SelectCommand = "SELECT * FROM Beaches WHERE " & buttonStr & " LIKE '%" & searchStr & "%'"
        SqlDataSource2.DataBind()
        Return True
    End Function

    Protected Sub lb_Likes_Click(sender As Object, e As EventArgs) Handles lb_Likes.Click
        SqlDataSource2.SelectCommand = "SELECT * FROM BeachesWithMostLikes ORDER BY TotalLikes DESC"
        SqlDataSource2.DataBind()
        'Change the text of heading
        myHeading.InnerHtml = "Most Liked Beaches"
    End Sub

    Protected Sub lb_Rating_Click(sender As Object, e As EventArgs) Handles lb_Rating.Click
        SqlDataSource2.SelectCommand = "SELECT * FROM BeachesWithAverageRating ORDER BY AverageRating DESC"
        SqlDataSource2.DataBind()
        'Change the text of heading
        myHeading.InnerHtml = "Highest Rated Beaches"
    End Sub

    Protected Sub lb_Views_Click(sender As Object, e As EventArgs) Handles lb_Views.Click
        SqlDataSource2.SelectCommand = "SELECT * FROM BeachesWithMostViews ORDER BY TotalViews DESC"
        SqlDataSource2.DataBind()
        'Change the text of heading
        myHeading.InnerHtml = "Most Viewed Beaches"
    End Sub

    Protected Sub lb_Flag_Click(sender As Object, e As EventArgs) Handles lb_Flag.Click
        SqlDataSource2.SelectCommand = "SELECT * FROM [Beaches] WHERE BlueFlag = True ORDER BY DateAdded DESC"
        SqlDataSource2.DataBind()
        'Change the text of heading
        myHeading.InnerHtml = "Blue Flagged Beaches"
    End Sub

    Protected Sub lb_Comments_Click(sender As Object, e As EventArgs) Handles lb_Comments.Click
        SqlDataSource2.SelectCommand = "SELECT * FROM BeachesWithMostComments ORDER BY TotalComments DESC"
        SqlDataSource2.DataBind()
        'Change the text of heading
        myHeading.InnerHtml = "Most discussed Beaches"
    End Sub

    Protected Sub SqlDataSource2_Selected(sender As Object, e As System.Web.UI.WebControls.SqlDataSourceStatusEventArgs) Handles SqlDataSource2.Selected
        'If datasource is empty
        If (e.AffectedRows < 1) Then
            myHeading.InnerHtml = "No Beach found"
        End If
        If (e.AffectedRows < 13) Then
            DataPagerBeaches.Visible = False
        Else
            DataPagerBeaches.Visible = True
        End If
    End Sub

    Protected Sub ListView1_DataBound(sender As Object, e As EventArgs)
        'Display datapager only if there is more than one page to display
        DataPagerBeaches.Visible = (DataPagerBeaches.PageSize < DataPagerBeaches.TotalRowCount)
    End Sub

    Protected Function getViews(ByVal beachID As Integer) As String
        'Return the total views for this page.
        Dim result As String = ""
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("BeachConnectionString").ConnectionString)
        Dim SqlString As String = "SELECT * FROM TotalViewsForAllBeaches WHERE ID = @f1"
        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@f1", beachID)
        oleDbConn.Open()
        Dim dr As OleDbDataReader = cmd.ExecuteReader()
        dr.Read()
        result = dr("TotalViews")
        oleDbConn.Close()
        Return result
    End Function
End Class